package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppNeed;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppServices;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppSuccessExample;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.mapper.DxshAppNeedMapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.*;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiBusiness.BusinessRpDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiUser.GetUserIdDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddUser.apiUser.GetMidongdongUserDto;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.BusinessRestService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserRestService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppNeedService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppServicesService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppSuccessExampleService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.services.NeedRqVo;
import com.syni.mdd.sjb.common.datatreaure.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-07-29
 */
@Service
public class DxshAppNeedServiceImpl extends ServiceImpl<DxshAppNeedMapper, DxshAppNeed> implements DxshAppNeedService {

    @Autowired
    UserRestService userRestService;
    @Autowired
    BusinessRestService businessRestService;
    @Autowired
    DxshAppNeedService dxshAppNeedService;
    @Autowired
    DxshAppServicesService dxshAppServicesService;
    @Autowired
    DxshAppSuccessExampleService dxshAppSuccessExampleService;
    @Autowired
    TokenUtils tokenUtils;


    @Override
    public Map<String, Object> saveBusinessNeed(NeedRqVo needRqVo) {
        Integer userId = tokenUtils.checkUser(RequestHeaderData.getRequestHeaderData().getString("token"));
        DxshAppNeed dxshAppNeed = new DxshAppNeed();
        BeanUtils.copyProperties(needRqVo,dxshAppNeed);
        dxshAppNeed.setNewTime(DateTimeUtils.converDateToLocalDateTime(new Date()));
        GetMidongdongUserDto getMidongdongUserDto = new GetMidongdongUserDto();
        getMidongdongUserDto.setUserId(userId);
        RpcResponseVo rpcResponseVo = userRestService.getMidongdongUser(getMidongdongUserDto);
        if(rpcResponseVo.isSuccess()) {
            getMidongdongUserDto = rpcResponseVo.parseObject(GetMidongdongUserDto.class);
            dxshAppNeed.setPhone(getMidongdongUserDto.getPhone());
        }
        if(needRqVo.getPhone() != null){
            RpcResponseVo userInfoByPhone = userRestService.getUserInfoByPhone(needRqVo.getPhone());
            if(userInfoByPhone.isSuccess()){
                GetUserIdDto getUserIdDto = userInfoByPhone.parseObject(GetUserIdDto.class);
                if(getUserIdDto != null){
                    userId = getUserIdDto.getUserId();
                }
            }
        }
        if(userId != null){
            RpcResponseVo userClaimBusiness = businessRestService.getUserClaimBusiness(userId, Constants.COMMON_FLAG_FALSE_INT);
            if(userClaimBusiness.isSuccess()) {
                List<BusinessRpDto> businessRpDtos = JSON.parseArray(JSON.toJSONString(userClaimBusiness.getData()), BusinessRpDto.class);
                if(businessRpDtos.size()>0){
                    dxshAppNeed.setIsRegisterBusiness(Constants.COMMON_FLAG_TRUE_INT);
                    if(needRqVo.getBusinessName() == null){
                        dxshAppNeed.setBusinessName(businessRpDtos.get(0).getVendorName());
                    }
                }
            }
        }
        dxshAppNeed.setUserId(userId);
        dxshAppNeedService.saveOrUpdate(dxshAppNeed);

        if(dxshAppNeed.getServiceId() != null){
            QueryWrapper queryWrapper = new QueryWrapper();
            switch (dxshAppNeed.getServiceType()){
                case 0:
                    //套餐服务
                    queryWrapper.eq(DxshAppServices.ID,dxshAppNeed.getServiceId());
                    DxshAppServices dxshAppServices = dxshAppServicesService.getOne(queryWrapper);
                    dxshAppServices.setClueNum(dxshAppServices.getClueNum()+1);
                    dxshAppServicesService.saveOrUpdate(dxshAppServices);
                    break;
                case 1:
                    //案例服务
                    queryWrapper.eq(DxshAppSuccessExample.ID,dxshAppNeed.getServiceId());
                    DxshAppSuccessExample dxshAppSuccessExample = dxshAppSuccessExampleService.getOne(queryWrapper);
                    dxshAppSuccessExample.setClueNum(dxshAppSuccessExample.getClueNum()+1);
                    dxshAppSuccessExampleService.saveOrUpdate(dxshAppSuccessExample);
                    break;
            }
        }
        return ResultMapHelper.success(null, ResultMsg.HAND_SUCCESS);
    }
}
